package tongwentongshu.com.app.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.geocoder.RegeocodeResult;

import java.util.ArrayList;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import tongwentongshu.com.app.App;
import tongwentongshu.com.app.R;
import tongwentongshu.com.app.bean.AppItem;
import tongwentongshu.com.app.bean.BookTypeBean;
import tongwentongshu.com.app.bean.OrderEvent;
import tongwentongshu.com.app.bean.RqcordBook;
import tongwentongshu.com.app.contract.AgainShareBookContract;
import tongwentongshu.com.app.contract.CallBackColonyContract;
import tongwentongshu.com.app.contract.ShareBookContract;
import tongwentongshu.com.app.db.Cache;
import tongwentongshu.com.app.dialog.AlertUtils;
import tongwentongshu.com.app.model.ShareBookModel;
import tongwentongshu.com.app.presenter.AgainShareBookPresenter;
import tongwentongshu.com.app.presenter.ShareBookPresenter;
import tongwentongshu.com.app.utils.GlideUtil;
import tongwentongshu.com.app.utils.ToastUtil;
import tongwentongshu.com.app.zxing.CaptureActivity;

import static tongwentongshu.com.app.App.CODE;
import static tongwentongshu.com.app.App.CaptureCode;
import static tongwentongshu.com.app.App.NAME;
import static tongwentongshu.com.app.App.POINT;
import static tongwentongshu.com.app.App.SelectLocationCode;

/**
 * 分享图书页面
 * Created by XiongBin on 2017/6/27.
 */

public class AgainShareBookActivity extends BaseActivity implements AgainShareBookContract.View {
    @BindView(R.id.title_complete)
    TextView complete;//确认分享
    @BindView(R.id.iv_cover)
    ImageView cover;//封面图
    @BindView(R.id.tv_book_type)
    TextView book_type;//书籍类别
    @BindView(R.id.tv_new_old)
    TextView old_degree;//新旧程度
    @BindView(R.id.tv_position)
    TextView now_position;//当前位置
    @BindView(R.id.et_price)
    TextView price;//价格
    @BindView(R.id.et_introduction)
    TextView introduction;//简介
    @BindView(R.id.et_book_name)
    TextView book_name;//书名


    private AgainShareBookPresenter againShareBookPresenter;
    private int old_ID = -1;//书籍类别id

    private PoiItem poiItem;//当前poi

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_again_sharebook;
    }

    @Override
    protected int getTitleId() {
        return R.string.pager_again_share;
    }

    @Override
    protected void initView() {
        App.getApplication().addReadingActivity(this);
        complete.setVisibility(View.VISIBLE);
        againShareBookPresenter = new AgainShareBookPresenter(this);

    }

    @Override
    protected void initData() {

        book_name.setText(getIntent().getExtras().getString("bookname1"));
        introduction.setText(getIntent().getExtras().getString("bookinfo"));
        price.setText(getIntent().getExtras().getString("money"));
        book_type.setText(getIntent().getExtras().getString("booktype"));
        old_degree.setText(getIntent().getExtras().getString("old_degree"));
        GlideUtil.setImage1(mContext, getIntent().getExtras().getString("bookpicurl"), R.drawable.default_graph, R.drawable.default_graph, cover);

        againShareBookPresenter.getAddress(this);
    }

    @Override
    protected void initListener() {
        super.initListener();
    }


    @OnClick({R.id.tv_position, R.id.tv_new_old, R.id.title_complete, R.id.tv_share_gui})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_new_old:
                AlertUtils.initWheelDialog(this, new_old_list, old_ID, onWheelBack);
                break;
            case R.id.tv_position:
                Bundle b = new Bundle();
                ArrayList<PoiItem> list = (ArrayList<PoiItem>) againShareBookPresenter.getPoiList();
                if (list != null)
                    b.putParcelableArrayList("poi_list", list);
                startActivityForResult(SelectLocationActivity.class, b, 0);
                break;
            case R.id.title_complete:
                submit();
                break;
            case R.id.tv_share_gui:
                Bundle b1 = new Bundle();
                b1.putString("url", "http://tongwentongshu.cn/index.php?g=&m=Index&a=get_share_rule&type=1");
                startActivity(WebViewActivity.class, b1);
                break;
        }
    }

    /**
     * 选取新旧程度、书籍类别弹窗监听
     */
    public CallBackColonyContract.OnWheelBack onWheelBack = new CallBackColonyContract.OnWheelBack() {

        @Override
        public void wheelCallBack(int index) {
            old_ID = index;
            old_degree.setText(new_old_list[index]);
            AlertUtils.dismiss();
        }
    };

    @Override
    public void onActivityResult(int requestCode, int resultCode,
                                 Intent data) {
        switch (resultCode) {
            case SelectLocationCode:
                Bundle locationBundle = data.getExtras();
                String poi_title = locationBundle.getString("poi_title");
                Log.e("tag", "poi_title=" + poi_title);
                setLocation(poi_title);
                break;
        }

    }

    /**
     * 权限选择后
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }


    public void onEventMainThread(OrderEvent result) {
        poiItem = result.getResult();
//        ToastUtil.show(mContext, result.getResult().getTitle());
        System.out.println("我收到了eventBus的消息");
    }

    @Override
    public void setLocation(String location_string) {
        now_position.setText(location_string);
    }


    @Override
    public void setPoiItem(PoiItem p) {
        poiItem = p;
    }

    private String[] new_old_list = {"全新", "九成", "八成", "七成", "六成", "五成", "四成", "三成"};

    @Override
    public void setBookType(BookTypeBean msg) {
        this.msg = msg;
        new_old_list = new String[msg.getData().size()];
        for (int i = 0; i < msg.getData().size(); i++) {
            new_old_list[i] = msg.getData().get(i).getName();
        }

    }

    @Override
    public void onSuccess(AppItem msg) {
//        ToastUtil.show(mContext, msg.msg);
        startActivity(MyReadingActivity.class);
        Bundle b = new Bundle();
        b.putString("bookid", getIntent().getExtras().getString("bookid"));
        b.putString("bookname", getIntent().getExtras().getString("bookname1"));
        b.putString("img", getIntent().getExtras().getString("bookpicurl"));
        startActivity(ShareCompleteActivity.class, b);
        App.getApplication().exitRead();
    }

    BookTypeBean msg;

    /**
     * 提交参数
     */
    private Map<String, String> map;
    private AMapLocation location;
    private RegeocodeResult result;

    private void submit() {
        map = new ArrayMap<>();

        map.put("token", Cache.getToken());//token
        map.put("bookid", getIntent().getExtras().getString("bookname"));//token
        map.put("book_new_old", old_degree.getText().toString().trim());//新旧程度
        location = againShareBookPresenter.getAMapLocation();
        map.put("province", location.getProvince());//省
        map.put("city_name", location.getCity());//市
        map.put("district_name", location.getDistrict());//区
        result = againShareBookPresenter.getRegeocodeResult();
        map.put("street_name", againShareBookPresenter.getStreet().get(NAME));//商圈名称
        map.put("stree_map", againShareBookPresenter.getStreet().get(POINT));//商圈坐标
        if (!TextUtils.isEmpty(againShareBookPresenter.getStreet().get(CODE))) {
            map.put("stree_code", againShareBookPresenter.getStreet().get(CODE));//商圈编码
        }
        map.put("tradingarea_name", againShareBookPresenter.getTradingarea().get(NAME));//aois名称
        map.put("tradingarea_map", againShareBookPresenter.getTradingarea().get(POINT));//aois坐标
        map.put("tradingarea_code", againShareBookPresenter.getTradingarea().get(CODE));//sois编码
        map.put("book_postition", poiItem.getTitle());//当前位置
        map.put("book_postition_map", poiItem.getLatLonPoint().getLongitude() + "," + poiItem.getLatLonPoint().getLatitude());//当前位置坐标
        againShareBookPresenter.againShare(map);

    }

}
